package com.mySampleApplication.server;
public class SerialNumberGenerator {

    private int length; // 流水号长度
    private int step;   // 步长
    private int counter;    // 计数器
    private String format;  // 流水号格式

    public SerialNumberGenerator(int length, int step, String format) {
        this.length = length;
        this.step = step;
        this.format = format;
        this.counter = 0;
    }

    public String getNextSerialNumber() {
        String serialNumber = String.format(format, counter);
        counter += step;

        // 如果流水号长度超过指定长度，则截取长度，如果不足则在前面补0
        if (serialNumber.length() > length) {
            serialNumber = serialNumber.substring(serialNumber.length() - length);
        } else if (serialNumber.length() < length) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length - serialNumber.length(); i++) {
                sb.append("0");
            }
            sb.append(serialNumber);
            serialNumber = sb.toString();
        }

        return serialNumber;
    }

    public static void main(String[] args) {
        SerialNumberGenerator generator = new SerialNumberGenerator(6, 1, "%06d");
        for (int i = 0; i < 50; i++) {
            System.out.println(generator.getNextSerialNumber());
        }
    }
}
